//
//  FansViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/24.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "FansViewController.h"
#import "ZiLiaoTableViewCell.h"
#import "IntroViewController.h"
#import "IGGetFansMemberApi.h"
#import "IGFocusmember.h"
#import "IGArtGalleryAttentApi.h"

@interface FansViewController () <UITableViewDelegate, UITableViewDataSource> {
    NSString *followerId;
    NSString *type;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property (nonatomic, strong) IGGetFansMemberApi *getFansApi;
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation FansViewController
#pragma mark-懒加载
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
#pragma mark-生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"关注我的";
    self.title = NSLocalizedString(@"My Follower", nil);
    self.navigationController.navigationBarHidden = NO;
    self.table.backgroundColor = [UIColor c2Color];
    _pageNum = @"1";
    self.getFansApi = [IGGetFansMemberApi new];
    [self.table ig_setHeaderWithRefreshingBlock:^{
        _pageNum = @"1";
        [self dataRequest];
    }];
    [self dataRequest];
    
}
-(void)dataRequest{
    @KWeakObj(self)
    self.getFansApi.pageNum = _pageNum;
    self.getFansApi.uid = [IGUser getUid];
    [self.getFansApi startWithDecodeClass:[IGGetFansMemberResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.table ig_endHeaderRefreshing];
        [selfWeak.table ig_endFooterRefreshing];
        if (responseObject) {
            IGGetFansMemberResp *resp=responseObject;
            IGGetFansMemberModel *model=resp.data;
            if (model.fansMemberList&&model.fansMemberList.count>0) {
                [selfWeak setupFooter];
            } else {
                [selfWeak.table ig_endFooterRefreshingWithNoMoreData];
            }
            
            if ([_pageNum isEqualToString:@"1"])
            {
                _dataSource = [NSMutableArray arrayWithArray:model.fansMemberList];
            }
            else
            {
                [_dataSource addObjectsFromArray:model.fansMemberList];
            }
            [selfWeak.table reloadData];
            
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.table ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak dataRequest];
    }];
}

#pragma mark -Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZiLiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell = [[ZiLiaoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.data = _dataSource[indexPath.row];
    [cell followOwerAction:^(IGFocusmember *info) {
        followerId = info.otherId;
        self.attentApi = [[IGArtGalleryAttentApi alloc]init];
        _attentApi.operatorType = [info.attentionState boolValue]?@"0":@"1";
        __weak typeof(self) weakSelf = self;
        _attentApi.museumId = info.otherId;
        [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            if (responseObject) {
                if ([info.attentionState boolValue]) {
                    //                    [self presentMessageTips:@"关注成功"];
                    //                    [self presentMessageTips:NSLocalizedString(@"Follow Successfully", nil)];
                    
                } else {
                    //                    [self presentMessageTips:@"取消成功"];
                    //                    [self presentMessageTips:NSLocalizedString(@"Canceled Successfully", nil)];
                    
                }
                [weakSelf.table ig_beginHeaderRefreshing];
                //                [self dataRequest];
            } else {
                if ([info.attentionState boolValue]) {
                    //                    [self presentMessageTips:@"关注失败"];
                    [self presentMessageTips:NSLocalizedString(@"Follow Failed", nil)];
                } else {
                    //                    [self presentMessageTips:@"取消失败"];
                    [self presentMessageTips:NSLocalizedString(@"Cancel Failed", nil)];
                }
            }
        }];
    }];
    [cell setNeedsLayout];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZiLiaoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    IGFocusmember *info = cell.data;
    
    IntroViewController *vc = [[IntroViewController alloc] init];
    vc.u_id = info.otherId;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
@end
